﻿/*
* AlphaButtons
* Version 1.0
* @requires jQuery v1.4.2 or greater - jQuery 1.7+ recommended
*
* Copyright (c) 2013 Chad Schroeder
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
/**
* @description Creates alhpanumeric buttons under a specific element
* @type jQuery
* @name AlphaButtons
* @category Plugins/AlphaButtons
* @author Chad Schroeder http://chadschroeder.blogspot.com
*/
; (function ($) {
    $.alphaButtons = function (el, options) {
        var base = this;

        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("alphaButtons", base);

        base.init = function () {

            base.options = $.extend({}, $.alphaButtons.defaultOptions, options);

            base.update();

            if (base.options.relativeElement != null) {
                $(window).resize(function () {
                    setTimeout(function(){
                        base.update();
                    });
                });
            }
        };

        function getCharacters(){
            var lang = $("html").attr("lang");
            switch(lang)
            {
                case "es-es":
                    return ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","Ñ","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
                default:
                    return ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
            }
        };

        base.update = function () {
            base.$el.empty().css({ "width": (base.options.relativeElement == null ? base.$el.outerWidth() : base.options.relativeElement.outerWidth()) + "px", "position": "relative", "overflow": "hidden", "white-space": "nowrap" }).addClass("alphaButtons");
            var e = $("<ul />").appendTo(base.$el);
            var chars = getCharacters();
            for (var i = 0; i < chars.length; i++) {
                $("<li class=\"alphaButtons-li\" />").appendTo(e).text(chars[i]).click(function () {
                    base.options.onAlphaClick($(this).text());
                    $(document).trigger("alphaButtonClick", [$(this).text()]);
                });
            }

            if (base.el.offsetWidth < base.el.scrollWidth) {
                e.append("<a class=\"alphaButtons-li\" style=\"position: absolute; right: 0px;\">»</a>");
                $("a.alphaButtons-li").click(function () {
                    var btn = $(this);
                    var mLeft = parseInt(e.css("margin-left"));
                    var diff = el.scrollWidth - el.offsetWidth;
                    btn.hide();
                    e.animate({ "margin-left": (mLeft == 0 ? (el.offsetWidth - el.scrollWidth) : 0) + "px" }, function () {
                        btn.css({ "zoom": 1, "left": mLeft == 0 ? diff + "px" : "inherit", "right": mLeft == 0 ? "inherit" : "0px"}).html(mLeft == 0 ? "«" : "»").show();
                    });

                });
            }
        };

        base.init();
    };

    $.alphaButtons.defaultOptions = {
        relativeElement: null,
        onAlphaClick: $.noop
    };

    $.fn.alphaButtons = function (options) {
        return this.each(function () {
            (new $.alphaButtons(this, options));

        });
    };

})(jQuery);